package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.WylQz;
import com.corpcredit.service.GenericManager;

public class WylQzForm extends BasePage implements Serializable {
    private GenericManager<WylQz, Long> wylQzManager;
    private WylQz wylQz = new WylQz();
    private Long id;

    public void setWylQzManager(GenericManager<WylQz, Long> wylQzManager) {
        this.wylQzManager = wylQzManager;
    }

    public WylQz getWylQz() {
        return wylQz;
    }

    public void setWylQz(WylQz wylQz) {
        this.wylQz = wylQz;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        wylQzManager.remove(wylQz.getId());
        addMessage("wylQz.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            wylQz = wylQzManager.get(id);
        } else {
            wylQz = new WylQz();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (wylQz.getId() == null || wylQz.getId() == 0);
        // set username who act this operation, get the user name from httprequest
        wylQz.setUsername( getRequest().getRemoteUser() );
        // set operation date using the current time
        wylQz.setOptdate( new Date() );
        // set the operation type, default two types: add and update
        wylQz.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        wylQzManager.save(wylQz);

        String key = (isNew) ? "wylQz.added" : "wylQz.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            // set the id to the next page
            this.id = wylQz.getId();
            // return the update confirmation page
            return edit();
        }
    }
} 